<template>
    <div
        class="self-contnair"
        v-loading="isLoading"
        element-loading-background="rgba(250, 250, 250, 0.7)"
    >
        <!-- 自油分设置 -->
        <!-- <el-card
      shadow="never"
      v-loading="isLoading"
      element-loading-background="rgba(250, 250, 250, 0.7)"
    >
      <div slot="headerss" class="clearfix">
        <span>自油分设置</span>
        <el-button style="float: right; padding: 3px 20px;" type="text">关 闭</el-button>
        </div>-->
        <cardComponent pageTitle="自油分设置">
            <template slot="cardBody">
                <div class="form-row">
                    <el-form
                        ref="selfFormRef"
                        :rules="selfFormRules"
                        :model="selfForm"
                        label-width="150px"
                        size="small"
                    >
                        <el-row :gutter="25">
                            <el-col :span="12">
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="账户类别 : ">
                                            <el-input disabled v-model="selfForm.CATEGORY"></el-input>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="24">
                                        <el-form-item label="卡号 : ">
                                            <el-input disabled v-model="selfForm.CARDNBR"></el-input>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="24">
                                        <el-form-item label="自油分分期状态 : ">
                                            <el-input
                                                disabled
                                                v-model="selfForm.CUST_1"
                                                class="t-red"
                                            ></el-input>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="24">
                                        <el-form-item label="自油分分期情况 : ">
                                            <el-input disabled v-model="selfForm.MPGROUP_1"></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                            </el-col>
                            <el-col :span="12">
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="消费转分期起始金额 : ">
                                            <el-input disabled v-model="selfForm.PTMPAMT"></el-input>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="24">
                                        <el-form-item label="手续费收取方式 : " prop="OUTFLAG">
                                            <el-select
                                                :disabled="is3"
                                                v-model="selfForm.CPNOS2"
                                                placeholder="请选择手续费收取方式"
                                                width="100%"
                                            >
                                                <el-option label="一次性收取" value="1"></el-option>
                                                <el-option label="分摊收取" value="0"></el-option>
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="24">
                                        <el-form-item label="自动分期阈值 : " prop="PTMPAMT2">
                                            <el-select
                                                v-model="selfForm.PTMPAMT2"
                                                placeholder="请选择自动分期阈值"
                                                width="100%"
                                            >
                                                <el-option label="200" value="200"></el-option>
                                                <el-option label="500" value="500"></el-option>
                                                <el-option label="1000" value="1000"></el-option>
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="24">
                                        <el-form-item label="期数 : " prop="PTMPNBR2">
                                            <el-select
                                                v-model="selfForm.PTMPNBR2"
                                                placeholder="请选择期数"
                                                width="100%"
                                                @change="NBRMTH2Change"
                                            >
                                                <el-option label="3" value="003"></el-option>
                                                <el-option label="6" value="006"></el-option>
                                                <el-option label="9" value="009"></el-option>
                                                <el-option label="12" value="012"></el-option>
                                                <el-option label="18" value="018"></el-option>
                                                <el-option label="24" value="024"></el-option>
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                            </el-col>
                        </el-row>
                        <el-form-item size="small" label-width="0">
                            <el-button type="primary" size="small" @click="showEditInfor">修 改</el-button>
                            <!-- <el-button>关 闭</el-button> -->
                        </el-form-item>
                    </el-form>
                </div>
                <!-- </el-card> -->
            </template>
        </cardComponent>
    </div>
</template>

<script>
import { mapState } from 'vuex';
import cardComponent from '@/components/cardComponent/cardComponent.vue';
import { get_3xxx, get_4xxx } from '@/apis/getAPIClass';
import { setNumber } from '@/utils/Object';
import { getDictValue, getDictSelectList, getDictMappingValue } from '@/utils/Common';

export default {
    data() {
        return {
            selfForm: {
                CATEGORY: '', // 3052
                CARDNBR: '', // 3052
                CUSTRNAME: '', // 3052
                CUSTID: '', // 3001
                PTMPAMT: '', // 3001
                CPNOS2: '1',
                PTMPAMT2: '200',
                PTMPNBR2: '003',
                MPGROUP_1: '',
            },
            is3: true,
            selfFormRules: {
                PTMPAMT2: [
                    {
                        required: true,
                        message: '请选择自动分期阈值！',
                        trigger: 'change',
                    },
                ],
                PTMPNBR2: [
                    {
                        required: true,
                        message: '请选择期数',
                        trigger: 'change',
                    },
                ],
            },
            mp_group2: '',
            isLoading: false,
            MPGROUPList: '',
        };
    },
    name: 'selfautostallment',
    components: {
        cardComponent,
    },
    async mounted() {
        // 获取账户类别
        const MY_CATEGORY = await getDictMappingValue('CATE', this.cardInfo_3004.CATEGORY, this) || this.cardInfo_3004.CATEGORY;

        this.selfForm.CATEGORY = MY_CATEGORY;
        this.onLoadOBJ();
    },
    computed: mapState({
        defaultParams: state => state.common.defaultParams, // 接口默认参数
        cardInfo_3004: state => state.common.cardInfo_3004,
        cardList_3601: state => state.common.cardList_3601,
        pinFlag: state => state.common.pinFlag, // 是否密码检查标志
    }),
    methods: {
        async onLoadOBJ() {
            this.selfForm.CARDNBR = this.cardInfo_3004.CARDNO;
            this.MPGROUPList = await getDictSelectList('9922', this);
            this.get_3001();
        },
        async showEditInfor() {
            const confirmResult = await this.$confirm(
                '此操作将对自油分设置进行修改, 您确认需要修改吗？',
                '自油分提示',
                {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    customClass: 'warning', type: 'warning',
                },
            ).catch(err => err);
            if (confirmResult !== 'confirm') return this.$message.info('已取消自油分设置修改');


            await this.checkyes();
            await this.get_4078('28', '', false, false);
            await this.get_4078('06', '1', false, false);
            await this.get_4078('06', '2', true, true);
            // await this.get_3001();
        },
        NBRMTH2Change() {
            console.log(this.selfForm.PTMPNBR2, this.selfForm.CPNOS2);
            if (this.selfForm.PTMPNBR2 == '003') {
                this.is3 = true;
                this.selfForm.CPNOS2 = '1';
            } else {
                this.is3 = false;
            }
        },
        async get_3001() {
            // 3001 配置请求头
            const appBody_3001 = {
                CARDNO: this.cardInfo_3004.CARDNO || '',
                PINFLAG: this.pinFlag,
                FINAVAIYN: 0,
            };
            console.log(this.MPGROUPList);
            const result_3001 = await get_3xxx('3001', '自油分', appBody_3001, this);
            if (!result_3001) return false;
            this.selfForm.PTMPAMT = result_3001.body.appBody.PTMPAMT;
            this.selfForm.MPGROUP_1 = result_3001.body.appBody.MPGROUP;
            if (result_3001.body.appBody.MPGROUP == '') {
                this.selfForm.MPGROUP_1 = '购易贷12期分期收取';
            } else {
                this.MPGROUPList.forEach((item) => {
                    // console.log(item.code,result_3001.body.appBody.MPGROUP);
                    if (result_3001.body.appBody.MPGROUP * 1 == item.code * 1) {
                        console.log(item.code, result_3001.body.appBody.MPGROUP, item.label);
                        this.selfForm.MPGROUP_1 = item.label;
                    }
                });
            }
            // this.selfForm.MPGROUP_1 = result_3001.body.appBody.MPGROUP;
            this.selfForm.CUST_1 = '已开通';

            console.log(this.selfForm.PTMPAMT, result_3001.body.appBody.PTMPAMT);
        },
        async get_4078(SEVOPT, PTMPYN, FLAGE1, FLAGE2) {
            // 4078 配置请求头
            const appBody_4078 = {
                CARDNBR: this.selfForm.CARDNBR,
                PINFLAG: this.pinFlag,
                SEVOPT,
                CHKOPT: '0000000000',
                SERCNT: '999999999999999999999999999999',
                PURCHYN: '',
                CPNO: '0000', // 自动分期计划编号
                NBRMTH: '00', // 自动分期期数
                MINMPAMT: '00000000000', // 自动分期交易金额最小值
                MAXMPAMT: '00000000000', // 自动分期交易金额最大值
                PTMPYN,
                RMBBILL: 0,
                PTMPAMT: this.selfForm.PTMPAMT2,
                PTMPNBR: this.selfForm.PTMPNBR2,
                PTMPBEG: '00000000',
                PTMPEND: '00000000',
                MPGROUP: this.mp_group2,
                RTNRMBBILL: 0,
                CDPTMPYN: 1, // 修改选项为28时必填，0-默认 1-开通  2-关闭
            };
            console.log(appBody_4078);
            const result_4078 = await get_3xxx(
                '4078',
                '自动分期设置',
                appBody_4078,
                this,
                FLAGE1,
                FLAGE2,
            );
            console.log(result_4078);
            if (!result_4078) return false;
            if (FLAGE2) {
                this.onLoadOBJ();
                this.$alert('自动分期设置成功,请勿重复提交 !', '自动分期设置提示！', {
                    confirmButtonText: '确定',
                    center: true,
                    customClass: 'success ',
                    showClose: false,
                });
            }
        },
        checkyes() {
            const ptmp_nbr2 = this.selfForm.PTMPNBR2; // 分期期数
            const cpnos2 = this.selfForm.CPNOS2; // 手续费收取方式
            const ptmp_amt2 = this.selfForm.PTMPAMT; // 消费转起始金额
            console.log(ptmp_nbr2, cpnos2, ptmp_amt2);
            let mp_group2;
            if (cpnos2 == '0') {
                if (ptmp_nbr2 == '003') {
                    mp_group2 = '0001';
                }
                if (ptmp_nbr2 == '006') {
                    mp_group2 = '0002';
                }
                if (ptmp_nbr2 == '009') {
                    mp_group2 = '0003';
                }
                if (ptmp_nbr2 == '012') {
                    mp_group2 = '0004';
                }
                if (ptmp_nbr2 == '018') {
                    mp_group2 = '0005';
                }
                if (ptmp_nbr2 == '024') {
                    mp_group2 = '0006';
                }
            }
            if (cpnos2 == '1') {
                if (ptmp_nbr2 == '003') {
                    mp_group2 = '0007';
                }
                if (ptmp_nbr2 == '006') {
                    mp_group2 = '0008';
                }
                if (ptmp_nbr2 == '009') {
                    mp_group2 = '0009';
                }
                if (ptmp_nbr2 == '012') {
                    mp_group2 = '0010';
                }
                if (ptmp_nbr2 == '018') {
                    mp_group2 = '0011';
                }
                if (ptmp_nbr2 == '024') {
                    mp_group2 = '0012';
                }
            }
            this.mp_group2 = mp_group2;
            console.log(this.mp_group2, mp_group2);
        },
    },
};
</script>

<style  scoped lang="scss">
.self-contnair {
    /deep/.el-card__header {
        padding: 8px !important;
    }
    .form-row {
        width: 900px;
        margin: 0 auto;
        .el-select {
            width: 100%;
            // margin-bottom: 15px;
        }
    }
    .el-form-item {
        text-align: center;
        // margin-bottom: 10px;
    }
}
</style>
